<template>
	<view>
		<view class="container">
			<cu-custom bgColor="bg-gradual-orange" :isBack="true">
				<block slot="content">我的关注</block>
			</cu-custom>

			<!-- tab选择 -->
			<view class="tab-container">
				<view :class="['tab-item', { 'tab-item-active': selectTabIndex === index }]"
					v-for="(tab, index) in tabOptions" :key="index" @click="selectTab(index)">
					{{ tab }}
				</view>
			</view>

			<!-- 商品 -->
			<view class="product" v-if="selectTabIndex === 0">
				<view class="product-item" v-for="(product, index) in productData" :key="index"
					@tap="jumpTo('/pages/goods/details?id=' + product.id)">
					<image class="product-image" :src="product.image" mode=""></image>
					<view class="product-info">
						<view class="item-info-title">{{ product.title }}</view>
						<view class="item-info-price">
							<text>市场价</text>
							<text class="price">￥{{ product.price }}</text>
						</view>
						<view class="item-info-merchant">
							<image src="../../static/share.gif" mode=""></image>
							<view>付加加</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 商家 -->
			<view class="merchant" v-if="selectTabIndex === 1">
				<view class="businesses-item" v-for="(merchant, index) in merchantData" :key="index"
					@tap="jumpTo('/pages/home/detail?id=' + merchant.id)">
					<image class="item-left-image" :src="merchant.logo" mode=""></image>
					<view class="item-center-text">
						<view class="item-title">{{ merchant.title }}</view>
						<view class="item-phone">
							<image src="https://app.yycjkb.cn/index/fujiajia/phone.png" mode=""></image>
							{{ merchant.mobile }}
						</view>
					</view>
					<view class="item-right-icon">
						<image class="icon-red" src="https://app.yycjkb.cn/index/fujiajia/red.png" mode="aspectFill"></image>
						<view class="address">
							<image class="icon-address" src="https://app.yycjkb.cn/index/fujiajia/定位.png" mode="aspectFill"></image>
							{{ merchant.address }}
						</view>
					</view>
				</view>
			</view>

			<!-- 物料商品 -->
			<view class="product" v-if="selectTabIndex === 2">
				<view class="product-item" v-for="(product, index) in shopProductData" :key="index"
					@tap="jumpTo('/pages/shop/details?id=' + product.id)">
					<image class="product-image" :src="product.image" mode=""></image>
					<view class="product-info">
						<view class="item-info-title">{{ product.title }}</view>
						<view class="item-info-price">
							<text>市场价</text>
							<text class="price">￥{{ product.price }}</text>
						</view>
						<view class="item-info-merchant">
							<image src="../../static/share.gif" mode=""></image>
							<view>{{ product.merchantName }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import user from "@/api/user";

	export default {
		data() {
			return {
				tabOptions: ["商品", "商家", "物料商品"],
				selectTabIndex: 0,
				productData: [],
				merchantData: [],
				shopProductData: [],
				IMAGE_BASE_URL: "https://cos.yycjkb.cn"
			};
		},
		async created() {
			await this.loadProductData();
			await this.loadMerchantData();
			await this.loadShopProductData();
		},
		methods: {
			jumpTo(url) {
				uni.navigateTo({ url });
			},
			selectTab(index) {
				this.selectTabIndex = index;
				if (index === 0) this.loadProductData();
				else if (index === 1) this.loadMerchantData();
				else if (index === 2) this.loadShopProductData();
			},
			async loadProductData() {
				try {
					const res = await user.followProductList();
					if (res.code === 1) {
						this.productData = res.data.list.map(item => ({
							id: item.product_id,
							image: item.thumb ? this.IMAGE_BASE_URL + item.thumb : "/static/default.png",
							title: item.name || "未知商品",
							price: item.price || "暂无报价",
							merchantName: "付加加"
						}));
					} else {
						this.$toast(res.msg);
					}
				} catch (error) {
					console.error("获取关注商品失败:", error);
				}
			},
			async loadMerchantData() {
				try {
					const res = await user.followShopList();
					if (res.code === 1) {
						this.merchantData = res.data.list;
					} else {
						this.$toast(res.msg);
					}
				} catch (error) {
					console.error("获取关注商家失败:", error);
				}
			},
			async loadShopProductData() {
				try {
					const res = await user.followShopProductList();
					if (res.code === 1) {
						this.shopProductData = res.data.list.map(item => ({
							id: item.product_id,
							image: item.thumb ? this.IMAGE_BASE_URL + item.thumb : "/static/default.png",
							title: item.name || "未知商品",
							price: item.price || "暂无报价",
							merchantName: "付加加"
						}));
					} else {
						this.$toast(res.msg);
					}
				} catch (error) {
					console.error("获取关注物料商品失败:", error);
				}
			}
		}
	};
</script>

<style lang="less" scoped>
	.tab-container {
		display: flex;
		justify-content: space-around;
		background-color: #fff;
		.tab-item {
			font-size: 30rpx;
			padding: 30rpx 20rpx;
			width: 33.33%;
			text-align: center;
			position: relative;
			&.tab-item-active {
				color: #f76e00;
				&::before {
					content: "";
					position: absolute;
					left: 0;
					bottom: 0;
					width: 100%;
					height: 6rpx;
					background-color: #ffd1aa;
				}
			}
		}
	}
	.product {
		padding-top: 20rpx;
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(350rpx, 1fr));
		grid-gap: 20rpx;
		justify-items: center;
		.product-item {
			border-radius: 20rpx;
			overflow: hidden;
			width: 350rpx;
			height: 515rpx;
			background-color: #fff;
			.product-image {
				width: 100%;
				height: 350rpx;
			}
			.product-info {
				padding: 10rpx 20rpx;
				.item-info-title {
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.item-info-price {
					color: #7b7b7b;
					font-size: 26rpx;
					padding: 10rpx 0;
					.price {
						margin-left: 10rpx;
						color: #f76e00;
					}
				}
				.item-info-merchant {
					display: flex;
					align-items: center;
					font-size: 26rpx;
					color: #7b7b7b;
					image {
						width: 35rpx;
						height: 35rpx;
						border-radius: 100%;
						margin-right: 10rpx;
					}
				}
			}
		}
	}
	.merchant {
		.businesses-item {
			width: 100%;
			height: 204rpx;
			background-color: #fff;
			border-radius: 26rpx;
			margin-top: 20rpx;
			padding: 20rpx;
			display: flex;
			.item-left-image {
				width: 169rpx;
				height: 169rpx;
				border-radius: 26rpx;
			}
			.item-center-text {
				padding: 0 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.item-phone {
					display: flex;
					align-items: center;
					color: #7b7b7b;
					padding-top: 14rpx;
					image {
						width: 23rpx;
						height: 23rpx;
					}
				}
			}
			.item-right-icon {
				margin-left: auto;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.icon-red {
					width: 40rpx;
					height: 90rpx;
					margin-left: auto;
					margin-top: -10rpx;
				}
				.address {
					display: flex;
					align-items: center;
					color: #8a8a8a;
					image {
						height: 44rpx;
						width: 44rpx;
					}
				}
			}
		}
	}
</style>
